Stored Procedures [dbo].[asi_ReApplyPayments]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)Direction
@creditInvDistCreatedbit1Out
@debuggingbit1
SQL Script
CREATE                                            PROCEDURE [dbo].[asi_ReApplyPayments]
@creditInvDistCreated bit = 0 output,
@debugging    bit = 0 --False
AS
DECLARE @pmtKey uniqueidentifier
DECLARE @srcInvoiceKey uniqueidentifier
DECLARE @srcInvLineKey uniqueidentifier
DECLARE @pmtAppKey uniqueidentifier
DECLARE @orderNumber nvarchar(50)
DECLARE @orderLineNumber int
DECLARE @paymentScheduleLineKey uniqueidentifier
DECLARE @paymentNumber int
DECLARE @dueDate datetime
DECLARE @amountDue decimal(18,4)
DECLARE @outstandingBalance decimal(18,4)
DECLARE @glAccountKey uniqueidentifier
DECLARE @invoiceKey uniqueidentifier
DECLARE @invoiceLineKey uniqueidentifier
DECLARE @invoiceKeyTarget uniqueidentifier
DECLARE @invoiceLineKeyTarget uniqueidentifier
DECLARE @pmtAmt decimal(18,4)
DECLARE @quantitySold decimal(18,4)
DECLARE @finUnitKey uniqueidentifier
DECLARE @incomeAcctKey uniqueidentifier
DECLARE @ARAcctKey uniqueidentifier
DECLARE @amtAppliedToParentLine decimal(18,4)
DECLARE @amtToApplyToDistLine decimal(18,4)
DECLARE @invDistKey uniqueidentifier
DECLARE @priority int
DECLARE @lineAmountRemaining decimal(18,4)
DECLARE @applicationAmtRemaining decimal(18,4)
DECLARE @extIncome decimal(18,4)
DECLARE @extIncomeRecognized decimal(18,4)
DECLARE @acctMethod nvarchar(50)
DECLARE @appliedTotal decimal(18,4)
DECLARE @applyingCredit bit
DECLARE @componentIncome decimal(18,4)
DECLARE @unitIncome decimal(18,4)
DECLARE @actualAmount decimal(18,4)
DECLARE @discountTaken decimal(18,4)
DECLARE @monetaryAppKey uniqueidentifier
DECLARE @appAmount decimal(18,4)
DECLARE @discountRemaining decimal(18,4)
DECLARE @proportionedDiscount decimal(18,4)
DECLARE @zeroApplication bit
DECLARE @amountToApplyToScheduleLine decimal(18,4)
DECLARE @amountToApplyToDistribution  decimal(18,4)
DECLARE @minOfScheduleLineDistributionAmounts decimal(18,4)
DECLARE @amountToApply  decimal(18,4)
DECLARE @applyToPaymentScheduleFirst bit
DECLARE @transactionType int
DECLARE @transactionDate datetime
DECLARE @batchKey uniqueidentifier
DECLARE @invoiceNumber nvarchar(50)
SET @zeroApplication = 0
SET @applicationAmtRemaining = 0
set @lineAmountRemaining = 0
SET @amtAppliedToParentLine = 0
SET @pmtAmt = 0
/*
Read through the payments applied (at the order, invoice, order line, or invoice line level).
For each one:
    1. Create cursors to iterate over PaymentScheduleLines and #tmpInvoice lines -- the details of the
       cursors depends on whether the distribution is being done to an Invoice, Order, InvoiceLine,
       or OrderLine.
    2. If we are applying to Invoice or Order ("normal" application):
        a) for each PaymentScheduleLine with unrecognized income:
            i)  Note how much needs to be paid
            ii) For each InvoiceDistribution line, where the GLAccount matches the PaymentScheduleLine:
                - figure out how much to apply.  Each application is made to both a schedule line and an
                  distribution line, so the amount of each application is the lesser of the amount owed
                  for the current schedule line and the amount for the current distribution line
    Get the invoice distribution lines, ordered by pay priority of the invoice lines, for the invoice.
    For each line:
        - Create a new payment application for the line, for an amount up to
        the the non-recognized total income.
        - Decrement the counter keeping track of how much of the original
        application has been re-applied.
        - Make sure to also re-apply the unearned income (DiscountTaken) amounts
    End
    Delete the original application line.

   04/14/05 - rrk - Allow zero-amount monetary applications to create sales history
   06/24/05 - rrk - Re-worked so that there are two methods of applying payments:
                    1) The "normal" way, which is to apply to the PaymentScheduleLines in numerical order.
                       For each PaymentScheduleLine application, we
   
*/

        
DECLARE Get_Pmts CURSOR FOR
SELECT SrcPaymentKey, SrcInvoiceLineKey, ISNULL(Amount,0), MonetaryApplicationKey, ISNULL(AppliedAmount,0), OrderNumber,
OrderLineNumber,InvoiceKey,InvoiceLineKey,InvoiceDistributionKey, SrcInvoiceKey, DiscountTaken,
TransactionDate, TransactionType, ApplicationBatchKey
FROM #tmpPayments
WHERE OrderNumber IS NOT NULL OR OrderLineNumber IS NOT NULL OR InvoiceKey IS NOT NULL OR InvoiceLineKey IS NOT NULL
ORDER BY OrderNumber asc, OrderLineNumber desc, PaymentTypePriority asc, InvoiceLineKey desc
OPEN Get_Pmts
FETCH NEXT FROM Get_Pmts INTO @pmtKey,@srcInvLineKey, @pmtAmt,@pmtAppKey, @amtAppliedToParentLine,
@orderNumber, @orderLineNumber, @invoiceKey, @invoiceLineKey, @invDistKey, @srcInvoiceKey, @discountTaken,
@transactionDate, @transactionType, @batchKey
WHILE @@FETCH_STATUS = 0
BEGIN
   /* IF @amtAppliedToParentLine = 0
        SET @zeroApplication = 1
    ELSE
        SET @zeroApplication = 0 */

    SET @zeroApplication = 0
    
    SET @actualAmount = @amtAppliedToParentLine - @discountTaken
    SET @applicationAmtRemaining = @actualAmount
    SET @discountRemaining = @discountTaken
    IF @invDistKey is not null
        GOTO nextPmtApp
    
        
    IF @invoiceKey is not null AND @invoiceLineKey is null
    BEGIN
        -- Create cursors to iterate over PaymentScheduleLines and InvoiceDistributions, based on
        -- invoice key.
        SET @applyToPaymentScheduleFirst = 1
        DECLARE ApplyToScheduleLines CURSOR FOR
            SELECT PaymentScheduleLineKey, PaymentNumber, DueDate, AmountDue, OutstandingBalance, GLAccountKey
            FROM PaymentScheduleLine psl
            INNER JOIN InvoiceMain im on psl.InvoiceKey = im.InvoiceKey
            WHERE im.InvoiceKey =  @invoiceKey AND OutstandingBalance > 0
            ORDER BY PaymentNumber ASC, PayPriority ASC
        
        DECLARE ApplyToDistributionLines cursor for
            SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, OrderLineNumber, InvoiceNumber
            FROM #tmpInvoice
            WHERE #tmpInvoice.InvoiceKey =  @invoiceKey
            ORDER BY PayPriority ASC
    END
    ELSE IF @orderNumber is not null AND LEN(@orderNumber) > 0 AND (@orderLineNumber is null OR @orderLineNumber <= 0)
    BEGIN
        SET @applyToPaymentScheduleFirst = 1
        -- Create cursors to iterate over PaymentScheduleLines and InvoiceDistributions, based on
        -- invoice key.
        DECLARE ApplyToScheduleLines CURSOR FOR
            SELECT PaymentScheduleLineKey, PaymentNumber, DueDate, AmountDue, OutstandingBalance, GLAccountKey
            FROM  PaymentScheduleLine psl
            INNER JOIN InvoiceMain im on psl.InvoiceKey = im.InvoiceKey
            WHERE im.OrderNumber = @orderNumber AND OutstandingBalance > 0
            ORDER BY PaymentNumber ASC, PayPriority ASC
        
        DECLARE ApplyToDistributionLines cursor for
            SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, OrderLineNumber, InvoiceNumber
            FROM #tmpInvoice
            WHERE OrderNumber = @orderNumber
            ORDER BY PayPriority ASC
    END
    ELSE IF @invoiceLineKey is not null
    BEGIN
        SET @applyToPaymentScheduleFirst = 0
        SELECT @componentIncome = SUM(ISNULL(ExtendedIncome,0)) from #tmpInvoice
        WHERE ParentInvoiceLineKey = @invoiceLineKey
        IF @componentIncome > 0
        BEGIN
            DECLARE GetApplyToLines cursor for
                SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, PayPriority, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, OrderLineNumber, InvoiceNumber
                FROM #tmpInvoice
                WHERE ParentInvoiceLineKey = @invoiceLineKey
                ORDER BY PayPriority ASC
        END
        ELSE
        BEGIN
            DECLARE GetApplyToLines cursor for
                SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, PayPriority, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, OrderLineNumber, InvoiceNumber
                FROM #tmpInvoice
                WHERE InvoiceLineKey = @invoiceLineKey
                ORDER BY PayPriority ASC
        END
        DECLARE ApplyToScheduleLines CURSOR FOR
            SELECT PaymentScheduleLineKey, PaymentNumber, DueDate, AmountDue, OutstandingBalance, GLAccountKey
            FROM  InvoiceLine il
            INNER JOIN PaymentScheduleLine psl on psl.InvoiceKey = il.InvoiceKey
            WHERE il.InvoiceLineKey = @invoiceLineKey AND OutstandingBalance > 0
            ORDER BY psl.PaymentNumber ASC, psl.PayPriority ASC
    END
    ELSE IF @orderLineNumber is not null AND @orderLineNumber > 0
    BEGIN
        SET @applyToPaymentScheduleFirst = 0
        SELECT @componentIncome = SUM(ISNULL(ExtendedIncome,0)) from #tmpInvoice
        WHERE ParentInvoiceLineKey in
        (SELECT InvoiceLineKey
        FROM #tmpInvoice WHERE OrderLineNumber = @orderLineNumber AND OrderNumber = @orderNumber)
        IF @componentIncome > 0
        BEGIN
            DECLARE GetApplyToLines cursor for
                SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, PayPriority, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, @orderLineNumber, InvoiceNumber
                FROM #tmpInvoice
                WHERE ParentInvoiceLineKey in
                (SELECT InvoiceLineKey FROM #tmpInvoice WHERE OrderLineNumber = @orderLineNumber AND OrderNumber = @orderNumber)
                ORDER BY PayPriority ASC
        END
        ELSE
        BEGIN
            DECLARE GetApplyToLines cursor for
                SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, PayPriority, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, @orderLineNumber, InvoiceNumber
                FROM #tmpInvoice
                WHERE OrderLineNumber = @orderLineNumber  AND OrderNumber = @orderNumber
                ORDER BY PayPriority ASC
        END
        DECLARE ApplyToScheduleLines CURSOR FOR
            SELECT PaymentScheduleLineKey, PaymentNumber, DueDate, AmountDue, OutstandingBalance, GLAccountKey
            FROM  InvoiceLine il
            INNER JOIN PaymentScheduleLine psl on psl.InvoiceKey = il.InvoiceKey
            WHERE il.InvoiceLineKey in
                (SELECT InvoiceLineKey from #tmpInvoice
                    WHERE OrderLineNumber = @orderLineNumber)
                AND OutstandingBalance > 0
            ORDER BY psl.PaymentNumber ASC, psl.PayPriority ASC
    END
    ELSE
        GOTO nextPmtApp
        
    IF     @applyToPaymentScheduleFirst = 1
    BEGIN
        /* Method 1: apply to payment schedule lines in order, and then to distributions.
        */

        OPEN ApplyToScheduleLines
        FETCH next from ApplyToScheduleLines into
            @paymentScheduleLineKey, @paymentNumber, @dueDate, @amountDue, @outstandingBalance, @glAccountKey
        WHILE @@FETCH_STATUS = 0 AND (ABS(@applicationAmtRemaining) > 0 OR ABS(@discountRemaining) > 0 OR @zeroApplication = 1)
        BEGIN
            SET @amountToApplyToScheduleLine = @outstandingBalance
                
             OPEN ApplyToDistributionLines
            FETCH next from ApplyToDistributionLines into
                @invoiceKeyTarget, @invoiceLineKeyTarget, @invDistKey, @extIncome, @extIncomeRecognized, @acctMethod,
                @appliedTotal, @applyingCredit, @quantitySold, @finUnitKey, @incomeAcctKey, @ARAcctKey,
                @unitIncome, @orderLineNumber, @invoiceNumber
            WHILE @@FETCH_STATUS = 0 AND
                @amountToApplyToScheduleLine > 0 AND
                (ABS(@applicationAmtRemaining) > 0 OR ABS(@discountRemaining) > 0 OR @zeroApplication = 1)
            BEGIN
                IF @invDistKey is null OR @ARAcctKey != @glAccountKey
                    GOTO FetchNextDistribution
            
                IF @applicationAmtRemaining > 0 OR @discountRemaining > 0
                    SET @amountToApplyToDistribution = @extIncome - @appliedTotal
                ELSE    /* (@applicationAmtRemaining < 0 OR @discountRemaining < 0 - this is a negative payment) */
                    SET @amountToApplyToDistribution = @appliedTotal
                
                /* nothing more to do with this distribution */
                IF @amountToApplyToDistribution = 0
                    GOTO FetchNextDistribution
                
                /* find the minium of schedule line and distribution amounts */
                IF ABS(@amountToApplyToScheduleLine) <= ABS(@amountToApplyToDistribution)
                    SET @minOfScheduleLineDistributionAmounts = @amountToApplyToScheduleLine
                ELSE
                    SET @minOfScheduleLineDistributionAmounts = @amountToApplyToDistribution
                                    
                SET @amountToApply = @minOfScheduleLineDistributionAmounts
            
                IF ABS(@applicationAmtRemaining) < ABS(@amountToApply)
                    SET @amountToApply = @applicationAmtRemaining
                
                
                /* Handle early payment discounts */
                IF ABS(@discountRemaining) > 0        /* (could be a reversal of discount - on a negative pmt) */
                BEGIN
                    IF @discountRemaining > 0
                        SET @proportionedDiscount = @minOfScheduleLineDistributionAmounts - @amountToApply
                    ELSE /* (negative / reversal of discount... - note that this is assuming a zero or negative amount being applied by @amtToApplyToDistLine) */
                        SET @proportionedDiscount = -1 * (@minOfScheduleLineDistributionAmounts + @amountToApply)
                    IF ABS(@proportionedDiscount) > ABS(@discountRemaining)
                        SET @proportionedDiscount = @discountRemaining
            
                    SET @discountRemaining = @discountRemaining - @proportionedDiscount
                END
                ELSE
                    SET @proportionedDiscount = 0    
                    
        
                INSERT MonetaryApplication
                    (MonetaryApplicationKey, SrcPaymentKey, SrcInvoiceLineKey, Amount, InvoiceKey, InvoiceLineKey,
                        InvoiceDistributionKey, DiscountTaken, OrderNumber, OrderLineNumber, PaymentScheduleLineKey,
                        TransactionDate, TransactionType, BatchKey, InvoiceNumber)
                VALUES (NEWID(), @pmtKey, @srcInvLineKey, @amountToApply, @invoiceKeyTarget, @invoiceLineKeyTarget,
                        @invDistKey, @proportionedDiscount, @orderNumber, @orderLineNumber, @paymentScheduleLineKey,
                        @transactionDate, @transactionType, @batchKey, @invoiceNumber)
                SET @applicationAmtRemaining = @applicationAmtRemaining - @amountToApply
                SET @amountToApplyToScheduleLine = @amountToApplyToScheduleLine - @amountToApply
                UPDATE #tmpInvoice SET AppliedTotal = AppliedTotal + @amountToApply
                    WHERE InvoiceDistributionKey = @invDistKey
                UPDATE PaymentScheduleLine SET OutstandingBalance = OutstandingBalance - (@amountToApply + @proportionedDiscount)
                    WHERE PaymentScheduleLineKey = @paymentScheduleLineKey
                
                FetchNextDistribution:
                FETCH next from ApplyToDistributionLines into
                    @invoiceKeyTarget, @invoiceLineKeyTarget, @invDistKey, @extIncome, @extIncomeRecognized, @acctMethod,
                    @appliedTotal, @applyingCredit, @quantitySold, @finUnitKey, @incomeAcctKey, @ARAcctKey,
                    @unitIncome, @orderLineNumber, @invoiceNumber
            END
            CLOSE ApplyToDistributionLines
        
            FETCH next from ApplyToScheduleLines into
                @paymentScheduleLineKey, @paymentNumber, @dueDate, @amountDue, @outstandingBalance, @glAccountKey
        END
        CLOSE ApplyToScheduleLines
        DEALLOCATE ApplyToScheduleLines
        DEALLOCATE ApplyToDistributionLines
    
    END
    ELSE
    BEGIN
        /* Method 2: apply to distributions by invoice line, then to payment schedule lines
        */

        OPEN GetApplyToLines
        FETCH next from GetApplyToLines into
        @invoiceKeyTarget, @invoiceLineKeyTarget, @invDistKey, @priority, @extIncome, @extIncomeRecognized, @acctMethod, @appliedTotal, @applyingCredit, @quantitySold, @finUnitKey, @incomeAcctKey, @ARAcctKey, @unitIncome, @orderLineNumber, @invoiceNumber
            /* These will be ordered by pay priority... */
        WHILE @@FETCH_STATUS = 0 AND (ABS(@applicationAmtRemaining) > 0 OR ABS(@discountRemaining) > 0 OR @zeroApplication = 1)
        BEGIN
            IF @invDistKey is null
                GOTO nextApplyToLine
                    
            OPEN ApplyToScheduleLines
            FETCH next from ApplyToScheduleLines into
                @paymentScheduleLineKey, @paymentNumber, @dueDate, @amountDue, @outstandingBalance, @glAccountKey
            WHILE @@FETCH_STATUS = 0
            BEGIN
            
                IF @applicationAmtRemaining > 0 OR @discountRemaining > 0
                    SET @lineAmountRemaining = @extIncome - @appliedTotal
                ELSE    /* (@applicationAmtRemaining < 0 OR @discountRemaining < 0 - this is a negative payment) */
                    SET @lineAmountRemaining = @appliedTotal
            
                IF @lineAmountRemaining <=0 AND @zeroApplication = 0
                    GOTO nextApplyToLine   /* can't apply any amount(s) to this line... */
                /*
                   If the distribution line amount yet to be applied is greater than or equal to
                   (in absolute value) the payment being applied, then we can apply *all* of the
                   payment being applied  so init the "amount to apply to the distribution line"
                   to the entire payment being applied.
                */
    
                IF @lineAmountRemaining >= ABS(@applicationAmtRemaining)
                    SET @amtToApplyToDistLine = @applicationAmtRemaining
                ELSE
                /*
                    Otherwise, the distribution line amount yet to be applied is less than
                    (in absolute value) the payment being applied, so we can only apply
                    some of the payment
                */

                BEGIN
                    SET @amtToApplyToDistLine = @lineAmountRemaining
                    IF @applicationAmtRemaining < 0
                        SET @amtToApplyToDistLine = -1 * @amtToApplyToDistLine
                END
                
            
                IF @outstandingBalance < @amtToApplyToDistLine
                    SET @amtToApplyToDistLine = @outstandingBalance
                
                    /* Handle early payment discounts */
                IF ABS(@discountRemaining) > 0        /* (could be a reversal of discount - on a negative pmt) */
                BEGIN
                    IF @discountRemaining > 0
                        SET @proportionedDiscount = @lineAmountRemaining - @amtToApplyToDistLine
                    ELSE /* (negative / reversal of discount... - note that this is assuming a zero or negative amount being applied by @amtToApplyToDistLine) */
                        SET @proportionedDiscount = -1 * (@lineAmountRemaining + @amtToApplyToDistLine)
                    IF ABS(@proportionedDiscount) > ABS(@discountRemaining)
                        SET @proportionedDiscount = @discountRemaining
            
                    SET @discountRemaining = @discountRemaining - @proportionedDiscount
                END
                ELSE
                    SET @proportionedDiscount = 0
                
            
                INSERT MonetaryApplication
                    (MonetaryApplicationKey, SrcPaymentKey, SrcInvoiceLineKey, Amount, InvoiceKey, InvoiceLineKey,
                    InvoiceDistributionKey, DiscountTaken, OrderNumber, OrderLineNumber, PaymentScheduleLineKey,
                    TransactionDate, TransactionType, BatchKey, InvoiceNumber)
                    VALUES (NEWID(), @pmtKey, @srcInvLineKey, @amtToApplyToDistLine, @invoiceKeyTarget, @invoiceLineKeyTarget,
                    @invDistKey, @proportionedDiscount, @orderNumber, @orderLineNumber, @paymentScheduleLineKey,
                    @transactionDate, @transactionType, @batchKey, @invoiceNumber)
                SET @applicationAmtRemaining = @applicationAmtRemaining - @amtToApplyToDistLine
                -- update the amount of this distribution we applied to this schedule line
                SET @appliedTotal = @appliedTotal + @amtToApplyToDistLine
                UPDATE #tmpInvoice SET AppliedTotal = AppliedTotal + @amtToApplyToDistLine
                    WHERE InvoiceDistributionKey = @invDistKey
                UPDATE PaymentScheduleLine SET OutstandingBalance = OutstandingBalance - (@amtToApplyToDistLine + @proportionedDiscount)
                    WHERE PaymentScheduleLineKey = @paymentScheduleLineKey
                /* If this 'payment' line is actually a credit invoice line being applied, we need to insert a distribution line for the */
                /* credit invoice line to correspond to the amount being applied to the target invoice distribution line. */
                IF @applyingCredit = 1 AND @amtToApplyToDistLine != 0
                BEGIN
                    SET @creditInvDistCreated = 1
                    INSERT InvoiceDistribution (InvoiceDistributionKey, InvoiceKey, InvoiceLineKey, QuantitySold,
                    FinancialEntityKey, UnitIncome, ExtendedIncome, ExtendedIncomeRecognized, IncomeGLAccountKey, ARGLAccountKey)
                    VALUES (NEWID(), @srcInvoiceKey, @srcInvLineKey, @quantitySold, @finUnitKey, -1 * @unitIncome, -1 * @amtToApplyToDistLine, 0, @incomeAcctKey, @ARAcctKey)
                END
                FETCH next from ApplyToScheduleLines into
                    @paymentScheduleLineKey, @paymentNumber, @dueDate, @amountDue, @outstandingBalance, @glAccountKey
            END
            CLOSE ApplyToScheduleLines
            nextApplyToLine:
            FETCH next from GetApplyToLines into
            @invoiceKeyTarget, @invoiceLineKeyTarget, @invDistKey, @priority, @extIncome, @extIncomeRecognized, @acctMethod, @appliedTotal, @applyingCredit, @quantitySold, @finUnitKey, @incomeAcctKey, @ARAcctKey, @unitIncome, @orderLineNumber, @invoiceNumber
        END
        CLOSE GetApplyToLines
        DEALLOCATE GetApplyToLines
        DEALLOCATE ApplyToScheduleLines
    END
    -- -------------------------------------------------------------
    
    IF ABS(@applicationAmtRemaining) <= 0 AND ABS(@discountRemaining) <= 0
        DELETE FROM MonetaryApplication WHERE MonetaryApplicationKey = @pmtAppKey
    ELSE IF ABS(@applicationAmtRemaining) < ABS(@actualAmount) OR ABS(@discountRemaining) < ABS(@discountTaken)
        UPDATE MonetaryApplication SET Amount = @applicationAmtRemaining, DiscountTaken = @discountRemaining WHERE MonetaryApplicationKey = @pmtAppKey
nextPmtApp:
FETCH NEXT FROM Get_Pmts INTO @pmtKey,@srcInvLineKey, @pmtAmt,@pmtAppKey, @amtAppliedToParentLine,
@orderNumber, @orderLineNumber, @invoiceKey, @invoiceLineKey, @invDistKey, @srcInvoiceKey, @discountTaken,
@transactionDate, @transactionType, @batchKey
END
CLOSE Get_Pmts
DEALLOCATE Get_Pmts
IF @debugging = 1
BEGIN
PRINT 'MonetaryApplication: '
SELECT * from MonetaryApplication
END  --IF @debugging

GO
Uses
Used By